﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using MageWars.Support;
using Microsoft.Xna.Framework.Input;

namespace MageWars.GameElements
{
    public class PlayerMage : Mage
    {
        KeyboardState CurrentState = Keyboard.GetState(PlayerIndex.One);
        KeyboardState PreviousState = Keyboard.GetState(PlayerIndex.One);
        const Keys JumpKey = Keys.A;

        public PlayerMage()
        {
            this.Direction = MathHelper.Pi + MathHelper.PiOver2;
            this.HP = 100;
        }

        public override StateMachine GetSM()
        {
            return null;
        }

        private const float rotation = 0.2f;
        

        public override void UpdatePosition()
        {
            float sp = 0;
            IsWalking = false;

            CurrentState = Keyboard.GetState(PlayerIndex.One);

            if (CurrentState.IsKeyDown(Keys.Left))
            {
                IsWalking = true;
                this.Direction -= rotation;
            }
            if (CurrentState.IsKeyDown(Keys.Right))
            {

                IsWalking = true;
                this.Direction += rotation;
            }
            if (CurrentState.IsKeyDown(Keys.Up))
            {
                IsWalking = true;
                sp -= speed;
            }
            if (CurrentState.IsKeyDown(Keys.Down))
            {
                IsWalking = true;
                sp += speed;
            }
            
            if (IsWalking)
            {
                float x = Position.X + (float)Math.Cos(Direction) * sp;
                float y = Position.Y + (float)Math.Sin(Direction) * sp;

                Vector3 np = new Vector3(x, y, 0);
                Vector3 old_pos = Vector3.Zero;

                //Position = np;

                //Vector por si el mago choca
                float xx = Position.X - 10 * ((float)Math.Cos(Direction) * sp);
                float yy = Position.Y - 10 * ((float)Math.Sin(Direction) * sp);

                var nnv = new Vector3(xx, yy, 0);

                if (!CheckCollisions())
                    Position = np;
                else
                {
                    Position = nnv;
                }

            }
            else
            {

            }

            if (CurrentState.IsKeyDown(Keys.Enter))
            {
                Game1.ShowMesh = !Game1.ShowMesh;
            }

            if (CurrentState.IsKeyDown(Keys.Space))
            {
                Fire();
            }

            if (!IsJumping)
            {
                if (CurrentState.IsKeyDown(JumpKey) && !PreviousState.IsKeyDown(JumpKey))
                {
                    IsJumping = true;
                    Jump();
                }
            }
            else 
            {
                Jump();
            }

            PreviousState = CurrentState;
        }
    }
}
